//
//  GameScene.cpp
//  BattleRecipes
//
//  Created by Truong Hoang Son on 3/20/14.
//
//

#include "GameScene.h"
#include "MenuScene.h"
#include "SimpleAudioEngine.h"
#include "Header.h"
using namespace cocos2d;
using namespace CocosDenshion;

CCScene* GameScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    GameScene *layer = GameScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool GameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSprite* pSprite = CCSprite::create("gp.png");
    pSprite->setScaleX(SIZE.width/pSprite->getContentSize().width);
    pSprite->setScaleY(SIZE.height/pSprite->getContentSize().height);

    // position the sprite on the center of the screen
    pSprite->setPosition( ccp(SIZE.width/2, SIZE.height/2) );
    
    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);

    showPopup();
    
    return true;
}

void GameScene::showPopup(){
    //show Popup Confirm
    CCSprite* popupBG = CCSprite::create("popup-bg.png");
    popupBG->setScaleX(SIZE.width/2/popupBG->getContentSize().width);
    popupBG->setScaleY(SIZE.height/2/popupBG->getContentSize().height);
    popupBG->setTag(kTAG_POPUP);
    popupBG->setPosition(ccp(SIZE.width/2, SIZE.height/2));
    addChild(popupBG,999);
    CCPoint center = ccp(popupBG->getContentSize().width/2, popupBG->getContentSize().height/2);
    ///add Label
    CCLabelTTF * title = CCLabelTTF::create("Confirm to start game ?", "MarkerFelt-Thin", SIZE.width/30, CCSize(popupBG->getContentSize().width*popupBG->getScaleX() - 40, 0), kCCTextAlignmentCenter);
    title->setAnchorPoint(CCPoint(0.5, 0.5));
    title->setPosition(ccp(center.x, center.y + title->getContentSize().height*2));
    popupBG->addChild(title);
    
    //
    CCMenuItemImage* itemStart = CCMenuItemImage::create("play-button.png", "play-hover.png", this, menu_selector(GameScene::setupGame));
    CCMenuItemImage* itemMainMenu = CCMenuItemImage::create("mainmenu-button.png", "mainmenu-hover.png", this, menu_selector(GameScene::backToMainMenu));
    
    CCMenu* menu = CCMenu::create(itemStart,itemMainMenu,NULL);
    menu->alignItemsHorizontally();
    menu->setPosition(ccp(center.x, center.y - itemMainMenu->getContentSize().height));
    popupBG->addChild(menu);
}

void GameScene::setupGame(){
   CCNode* node = getChildByTag(kTAG_POPUP);
    node->removeAllChildrenWithCleanup(true);
    node->removeFromParentAndCleanup(true);
    //////////////////////////////////////
    
}
void GameScene::backToMainMenu(){
    CCNode* node = getChildByTag(kTAG_POPUP);
    node->removeAllChildrenWithCleanup(true);
    node->removeFromParentAndCleanup(true);
    CCDirector::sharedDirector()->replaceScene(MenuScene::scene());
}